#include <iostream>
#include <poll.h>
#include <unistd.h>
#include <string.h>

int main()
{
    struct pollfd fds;
    fds.fd = 0;
    fds.events = POLLIN;
    fds.revents = 0;

    int timeout = 1000; // timeout -1 阻塞式等待，0非阻塞式等待

    for (;;)
    {
        int n = poll(&fds, 1, -1);
        switch (n)
        {
        case -1:
            std::cerr << "poll error" << std::endl;
            break;
        case 0:
            std::cout << "poll timeout..." << std::endl;
            break;
        default:
            std::cout << "有事件发生" << std::endl;
            if(fds.revents & POLLIN)
            {
                std::cout << fds.fd << "上面的读事件发生了" << std::endl;
                char buffer[128];
                ssize_t s = read(fds.fd, buffer, sizeof(buffer));
                if(s > 0)
                {
                    buffer[s] = '\0';
                    write(1, buffer, strlen(buffer));
                }
            }
            break;
        }
    }
    return 0;
}